package com.jianyun.jyzs.widget;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.CrmActivity;
import com.jianyun.jyzs.activity.CrmGetFunnelActivity;
import com.jianyun.jyzs.constant.SysConstant;


/**
 * Created by mlli on 2017/8/31.
 */

public class CrmSearch {
    private CrmActivity context;
    private TextView customerState;
    private EditText enterpriseName;
    private EditText enterpriseAddress;

    public CrmSearch(CrmActivity context) {
        this.context = context;
    }

    public void show(View drop) {
        View conentView = LayoutInflater.from(context).inflate(R.layout.popupwindow_crm_search, null);
        final PopupWindow popupWindow = new PopupWindow(conentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        enterpriseName = (EditText) conentView.findViewById(R.id.enterpriseName);
        enterpriseAddress = (EditText) conentView.findViewById(R.id.enterpriseAddress);
        customerState = (TextView) conentView.findViewById(R.id.customerState);
        ImageView customerStateCancal = (ImageView) conentView.findViewById(R.id.customerStateCancal);
        conentView.findViewById(R.id.disclick).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                popupWindow.dismiss();
            }
        });
        final EditText nearUser = (EditText) conentView.findViewById(R.id.nearUser);
        Button searchButton = (Button) conentView.findViewById(R.id.searchButton);
        Button cancelButton = (Button) conentView.findViewById(R.id.cancelButton);
        customerStateCancal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                customerState.setText(null);
            }
        });
        customerState.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                context.startActivityForResult(new Intent(context, CrmGetFunnelActivity.class), 100);
            }
        });
        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*context.search(enterpriseName.getText().toString().trim(),
                        enterpriseAddress.getText().toString().trim(),
                        customerState.getText().toString().trim(),
                        nearUser.getText().toString().toString()
                );*/
                popupWindow.dismiss();
            }
        });
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x4D000000));
        popupWindow.update();
        popupWindow.showAsDropDown(drop);

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == -1) {
            if (requestCode == 100 & data != null) {
                String stringExtra = data.getStringExtra(SysConstant.msg_key_string1);
                customerState.setText(stringExtra);
            }
        }
    }
}
